package net.towee.client.com.lobby.view;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.towee.client.ToweeGinjector;
import net.towee.client.com.lobby.presenter.LobbyProfilePresenter.Display;
import net.towee.client.i18n.ToweeConstants;
import net.towee.client.resources.ClientResource;
import net.towee.client.widgets.ExtFlowPanel;
import net.towee.client.widgets.LeftTabsWidget;
import net.towee.client.widgets.UnorderedListWidget;
import net.towee.model.account.Connection;
import net.towee.model.account.Gender;
import net.towee.model.account.SocialStatus;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class LobbyProfileView extends FlowPanel implements Display, ClickHandler {
	private final Button nextButton;
	private final Button closeButton;
	
	private final FlowPanel photoContainer;
	private final FlowPanel textContainer;
	private final UnorderedListWidget categoriesContainer;
	private final FlowPanel fieldsContainer;
	
	private FlowPanel arrow;
	private Widget selectedCat;
	
	private final Map<Widget, FlowPanel> fieldsPages;
	
	private final ClientResource resource;
	private final ToweeConstants constants;
	private final ToweeGinjector injector;
	
	@Inject
	public LobbyProfileView(ClientResource resource, ToweeConstants constants, ToweeGinjector injector){
		this.resource = resource;
		this.constants = constants;
		this.injector = injector;
		
		this.fieldsPages = new HashMap<Widget, FlowPanel>();
		
		this.nextButton = new Button( constants.lobbyFirstStepContinue() );
		this.closeButton = new Button( constants.lobbyClose() );
		
		photoContainer = new FlowPanel();
		textContainer = new FlowPanel();
		categoriesContainer = new UnorderedListWidget();
		fieldsContainer = new FlowPanel();
		
		add(photoContainer);
		add(textContainer);
		add(categoriesContainer);
		add(fieldsContainer);
		
		createPhotoContainer();
		createTextAndButtonsContainer();
		createProfileCategoriesContainer();
		createProfileFieldsContainer();
	}
	
	private void createProfileFieldsContainer() {
		fieldsContainer.setStyleName(resource.css().lobbyProfileFieldsContainer());
		fieldsContainer.add(new LobbyProfileWhoPage(resource, constants));
	}

	private void createProfileCategoriesContainer() {
		// Create tabs container
		LeftTabsWidget tabs = new LeftTabsWidget( resource );
		tabs.getElement().getStyle().setProperty("top", "240px");
		tabs.getElement().getStyle().setProperty("width", "200px");
		add(tabs);
		
		// Add tabs
		tabs.addTab("Кто вы", true);
	}
	
	@Override
	public void onClick(ClickEvent event) {
		Object source = event.getSource();
		
		if(source.equals(closeButton)){}
		else if(source.equals(nextButton)){}
		else if(selectedCat!=null && !selectedCat.equals(source)){
			selectedCat.removeStyleName( resource.css().lobbyProfileCategorySelected() );
			arrow.removeFromParent();
			
			((Widget)source).addStyleName( resource.css().lobbyProfileCategorySelected() );
			((HasWidgets)source).add(arrow);
			selectedCat = ((Widget)source);
		}
	}

	private void createTextAndButtonsContainer() {
		ExtFlowPanel header = new ExtFlowPanel(constants.lobbyProfileHeader(), true);
		ExtFlowPanel text = new ExtFlowPanel(constants.lobbyProfileText(), true);
		
		header.setStyleName(resource.css().h2());
		text.setStyleName(resource.css().normalText());
		
		closeButton.setStyleName(resource.css().lobbyCloseButton());
		nextButton.setStyleName(resource.css().lobbyNextStepButton());
		nextButton.getElement().getStyle().setProperty("marginLeft", "293px");
		
		textContainer.setStyleName(resource.css().lobbyProfileTextContainer());
		textContainer.add(header);
		textContainer.add(text);
		textContainer.add(closeButton);
		textContainer.add(nextButton);
	}

	private void createPhotoContainer() {
		photoContainer.setStyleName(resource.css().lobbyProfilePhotoContainer());
		injector.getAccountPhoto().go( photoContainer );
	}

	@Override
	public String getFirstName() {
		return ((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).getFirstName();
	}

	@Override
	public String getLastName() {
		return ((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).getLastName();
	}

	@Override
	public Date getDob() {
		return ((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).getDob();
	}

	@Override
	public Gender getGender() {
		return ((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).getGender();
	}

	@Override
	public String getStatus() {
		return "";
	}

	@Override
	public SocialStatus getSocialStatus() {
		return null;
	}

	@Override
	public String getLittleAbout() {
		return "";
	}

	@Override
	public List<Connection> getConnections() {
		return ((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).getConnections();
	}

	@Override
	public void setFirstName(String name) {
		((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).setFirstName(name);
	}

	@Override
	public void setLastName(String name) {
		((LobbyProfileWhoPage)fieldsContainer.getWidget(0)).setLastName(name);
	}

	@Override
	public void setDob(Date dob) {
		
	}

	@Override
	public void setGender(String name) {
		
	}

	@Override
	public HasClickHandlers getNextStepButton() {
		return nextButton;
	}

	@Override
	public HasClickHandlers getCloseButton() {
		return closeButton;
	}

	@Override
	public Map<String, String> getExtendedProfile() {
		return new HashMap<String, String>();
	}

	@Override
	public void setConnections(List<Connection> connections) {
		
	}

	@Override
	public void setLittleAbout(String about) {
		
	}

	@Override
	public void setSocialStatus(SocialStatus status) {
		
	}

	@Override
	public void setStatus(String status) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void setExtendedProfile(Map<String, String> extProfile) {
		// TODO Auto-generated method stub
		
	}
}
